<template>
  <div class="exam-materials-page">
    <!-- 使用统一的导航栏 -->
    <NavBar @toggle-sidebar="toggleSidebar" />

    <!-- 主内容区域 -->
    <main class="main-content">
      <div class="page-header">
        <h2 class="page-title">考试资料</h2>
        <p class="page-subtitle">考试相关学习资料</p>
      </div>

      <!-- 加载状态 -->
      <div v-if="loading" class="loading-state">
        <div class="loading-spinner"></div>
        <p>正在加载资料...</p>
      </div>

      <!-- 无资料状态 -->
      <div v-else-if="resources.length === 0" class="empty-state">
        <p>暂无可用资料</p>
      </div>

      <!-- 资料卡片列表 -->
      <div v-else class="materials-container">
        <div 
          v-for="resource in resources" 
          :key="resource.resourceId"
          class="material-card"
        >
          <div class="card-header">
            <div class="file-type-section">
              <span class="file-type-label">文件类型:</span>
              <span class="file-type-tag">{{ getFileTypeLabel(resource.type) }}</span>
            </div>
            <div class="status-section">
              <div class="status-indicator"></div>
              <span class="status-text">可用</span>
            </div>
          </div>
          
          <!-- 资源标题 -->
          <div class="resource-title">{{ resource.title }}</div>
          
          <div class="resource-info">
            <div class="resource-id">资源ID: {{ resource.resourceId }}</div>
            <div class="resource-date">创建时间: {{ formatDate(resource.createdAt) }}</div>
          </div>
          
          <div class="card-divider"></div>
          
          <div class="card-actions">
            <button 
              class="open-material-btn" 
              @click="openMaterial(resource.ossUrl)"
            >
              打开资料
            </button>
          </div>
        </div>
      </div>

      <!-- 使用说明 -->
      <div v-if="resources.length > 0" class="instructions-card">
        <h3 class="instructions-title">使用说明</h3>
        <ul class="instructions-list">
          <li>点击"打开资料"按钮将在新窗口中打开对应的学习资料</li>
          <li>建议在考试前仔细阅读所有相关资料</li>
          <li>PDF文件需要安装PDF阅读器才能正常查看</li>
          <li>视频教程建议在网络良好的环境下观看</li>
          <li>如遇到资料无法打开的问题，请联系管理员</li>
        </ul>
      </div>

      <!-- 底部操作按钮 -->
      <div v-if="resources.length > 0" class="bottom-actions">
        <button class="secondary-btn" @click="goBackToExams">返回考试页面</button>
        <!-- <button class="primary-btn" @click="openAllMaterials">打开全部资料</button> -->
      </div>

      <!-- 底部操作按钮（无资料时只显示返回按钮） -->
      <div v-if="resources.length === 0" class="bottom-actions">
        <button class="secondary-btn" @click="goBackToExams">返回考试页面</button>
      </div>

      <!-- 底部提示 -->
      <div class="footer-notice">
        考试时间以系统时间为准，请提前做好准备
      </div>
    </main>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { getExamResourcesAPI } from '../apis/examAPI/StudentExamsAPI'
import NavBar from "@/components/NavBar.vue"

const router = useRouter()
const route = useRoute()

// 切换侧边栏函数（NavBar需要）
const toggleSidebar = () => {
  // 这里可以添加侧边栏切换逻辑，或者留空
}

// 响应式数据
const resources = ref<Array<{
  resourceId: number;
  chatId: number;
  userId: number;
  ossUrl: string;
  type: string;
  createdAt: any;
  title?: string;
}>>([])

const loading = ref(false)

// 文件类型映射
const getFileTypeLabel = (type: string): string => {
  const typeMap: Record<string, string> = {
    '1': '视频',
    '2': 'PDF',
    '3': '文档',
    '4': '图片',
    '5': '音频'
  }
  return typeMap[type] || '其他'
}

// 格式化日期
const formatDate = (dateString: string): string => {
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

// 获取考试资源
const fetchExamResources = async () => {
  try {
    loading.value = true
    
    // 从路由参数获取examId，也可以从query参数获取
    const examId = route.params.examId || route.query.examId
    
    if (!examId) {
      console.error('未找到考试ID')
      return
    }
    
    const response = await getExamResourcesAPI(Number(examId))
    
    if (response.code === 200 && Array.isArray(response.data)) {
      resources.value = response.data
    } else {
      console.error('获取资源失败:', response.message)
    }
  } catch (error) {
    console.error('获取考试资源时出错:', error)
  } finally {
    loading.value = false
  }
}

// 格式化URL，确保包含协议
const formatUrl = (url: string): string => {
  if (!url.startsWith('http://') && !url.startsWith('https://')) {
    return 'https://' + url
  }
  return url
}

// 打开单个资料
const openMaterial = (ossUrl: string) => {
  if (ossUrl) {
    const fullUrl = formatUrl(ossUrl)
    window.open(fullUrl, '_blank')
  } else {
    alert('资料链接无效')
  }
}

// 打开全部资料
const openAllMaterials = () => {
  resources.value.forEach(resource => {
    if (resource.ossUrl) {
      const fullUrl = formatUrl(resource.ossUrl)
      window.open(fullUrl, '_blank')
    }
  })
}

// 返回考试页面
const goBackToExams = () => {
  router.push('/student-exam')
}

// 页面加载时获取数据
onMounted(() => {
  fetchExamResources()
})
</script>

<style scoped>
.exam-materials-page {
  position: relative;
  width: 100vw;
  min-height: 100vh;
  font-family: 'Inter', sans-serif;
  background: white;
  overflow-x: hidden;
}

/* 主内容区域 */
.main-content {
  position: relative;
  padding: 48px 24px;
  z-index: 5;
  max-width: 1272px;
  margin: 0 auto;
  width: 100%;
  box-sizing: border-box;
  margin-top: 64px; /* 为NavBar留出空间 */
}

.page-header {
  text-align: center;
  margin-bottom: 48px;
}

.page-title {
  font-weight: 700;
  font-size: 48px;
  line-height: 72px;
  color: #1E1E1E;
  margin: 0 0 16px 0;
}

.page-subtitle {
  font-weight: 400;
  font-size: 20px;
  line-height: 30px;
  color: #B3B3B3;
  margin: 0;
}

/* 加载和空状态 */
.loading-state, .empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #B3B3B3;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #2C2C2C;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 20px auto;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 资料容器 */
.materials-container {
  display: flex;
  flex-direction: column;
  gap: 24px;
  margin-bottom: 48px;
}

/* 资料卡片 */
.material-card {
  width: 100%;
  max-width: 540px;
  background: #FFFFFF;
  border: 1px solid #D9D9D9;
  border-radius: 8px;
  padding: 24px;
  margin: 0 auto;
  box-sizing: border-box;
  min-height: 180px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.file-type-section {
  display: flex;
  align-items: center;
  gap: 12px;
}

.file-type-label {
  font-weight: 400;
  font-size: 12px;
  line-height: 18px;
  color: #B3B3B3;
}

.file-type-tag {
  background: #E8EDF4;
  border-radius: 22369600px;
  padding: 1px 12px;
  font-weight: 400;
  font-size: 11px;
  line-height: 18px;
  color: #000000;
}

.status-section {
  display: flex;
  align-items: center;
  gap: 8px;
}

.status-indicator {
  width: 8px;
  height: 8px;
  background: #10B981;
  border-radius: 50%;
}

.status-text {
  font-weight: 400;
  font-size: 12px;
  line-height: 18px;
  color: #B3B3B3;
}

.resource-title {
  font-weight: 600;
  font-size: 18px;
  line-height: 27px;
  color: #1E1E1E;
  margin: 16px 0 12px 0;
  word-break: break-word;
}

.resource-info {
  display: flex;
  justify-content: space-between;
  margin-bottom: 12px;
  font-size: 12px;
  color: #666;
}

.card-divider {
  width: 100%;
  height: 1px;
  background: #F1F5F9;
  margin: 15px 0;
}

.card-actions {
  display: flex;
  justify-content: center;
}

.open-material-btn {
  width: 100%;
  max-width: 491px;
  height: 45px;
  background: #2C2C2C;
  border-radius: 8px;
  border: none;
  font-weight: 700;
  font-size: 14px;
  line-height: 21px;
  color: #FFFFFF;
  cursor: pointer;
  transition: background-color 0.3s;
}

.open-material-btn:hover {
  background: #3C3C3C;
}

/* 使用说明卡片 */
.instructions-card {
  width: 100%;
  max-width: 672px;
  background: #FFFFFF;
  border: 1px solid #D9D9D9;
  border-radius: 8px;
  padding: 33px 32px;
  margin: 0 auto 20px auto;
  box-sizing: border-box;
}

.instructions-title {
  font-weight: 700;
  font-size: 24px;
  line-height: 36px;
  color: #1E1E1E;
  text-align: center;
  margin: 0 0 19px 0;
}

.instructions-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.instructions-list li {
  font-weight: 400;
  font-size: 14px;
  line-height: 21px;
  color: #2C2C2C;
  margin-bottom: 12px;
  position: relative;
  padding-left: 0;
}

.instructions-list li:before {
  content: "• ";
  color: #2C2C2C;
  font-weight: bold;
}

.instructions-list li:last-child {
  margin-bottom: 0;
}

/* 底部操作按钮 */
.bottom-actions {
  display: flex;
  justify-content: center;
  gap: 49px;
  margin-bottom: 20px;
}

.secondary-btn, .primary-btn {
  height: 46px;
  border-radius: 8px;
  border: none;
  font-weight: 700;
  font-size: 14px;
  line-height: 21px;
  cursor: pointer;
  transition: all 0.3s;
}

.secondary-btn {
  width: 149px;
  background: #FFFFFF;
  border: 1px solid #D9D9D9;
  color: #2C2C2C;
}

.secondary-btn:hover {
  background: #F8FAFC;
  border-color: #2C2C2C;
}

.primary-btn {
  width: 148px;
  background: #2C2C2C;
  color: #FFFFFF;
}

.primary-btn:hover {
  background: #3C3C3C;
}

/* 底部提示 */
.footer-notice {
  text-align: center;
  font-weight: 400;
  font-size: 14px;
  line-height: 21px;
  color: #B3B3B3;
  margin-top: 20px;
}

/* 响应式调整 */
@media (max-width: 1300px) {
  .main-content {
    padding: 48px 20px;
  }
  
  .material-card, .instructions-card {
    width: 90%;
    max-width: 672px;
  }
  
  .bottom-actions {
    flex-direction: column;
    align-items: center;
    gap: 20px;
  }
}

@media (max-width: 768px) {
  .nav-menu {
    gap: 20px;
  }
  
  .page-title {
    font-size: 36px;
    line-height: 54px;
  }
  
  .material-card, .instructions-card {
    width: 95%;
  }
  
  .resource-title {
    font-size: 16px;
    line-height: 24px;
    margin: 12px 0 10px 0;
  }
  
  .resource-info {
    flex-direction: column;
    gap: 4px;
  }
}
</style>